package com.zhaowenhao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhaowenhao.bean.Employee;
import com.zhaowenhao.bean.Msg;
import com.zhaowenhao.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * Description:处理员工增删改查
 * User: zwh
 * Date: 2019-06-04
 * Time: 21:03
 */
@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;


    @RequestMapping(value = "/emps/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteEmp(@PathVariable(value = "ids") String ids) {
        if (ids.contains("-")) {
            List<Integer> integerList = new ArrayList<>();
            String[] list = ids.split("-");
            for (String id : list) {
                integerList.add(Integer.parseInt(id));
            }
            employeeService.batchDeleteEmp(integerList);
        } else {
            employeeService.deleteEmp(Integer.parseInt(ids));
        }
        return Msg.success();
    }

    @RequestMapping(value = "/emps/{empId}", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmp(Employee employee) {
        employeeService.updateEmp(employee);
        return Msg.success();
    }

    @RequestMapping(value = "/emps/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmpById(@PathVariable(value = "id") Integer id) {
        Employee employee = employeeService.getEmpById(id);
        return Msg.success().addExtend("emp", employee);
    }

    @ResponseBody
    @RequestMapping(value = "/checkuser", method = RequestMethod.POST)
    public Msg checkUser(@RequestParam(value = "empName") String empName) {
        boolean b = employeeService.checkUser(empName);
        if (b) {
            return Msg.success();
        } else {
            return Msg.failed();
        }
    }

    @RequestMapping(value = "/emps", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmp(@Valid Employee employee, BindingResult result) {
        System.out.println(employee.toString());
        if (result.hasErrors()) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            Map<String, Object> map = new HashMap<>();
            for (FieldError fieldError : fieldErrors) {
                System.out.println("错误字段名：" + fieldError.getField());
                System.out.println("错误信息：" + fieldError.getDefaultMessage());
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Msg.failed().addExtend("errorfields", map);
        } else {
            employeeService.saveEmp(employee);
            return Msg.success();
        }
    }

    @RequestMapping(value = "/emps", method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmpsWithJson(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
        //引入分页插件pagehelper
        //传入页码，以及分页大小
        PageHelper.startPage(pn, 5);
        //startpage后面紧跟的查询就是一个分页查询
        List all = employeeService.getAll();
        //使用 pageinfo封装数据信息
        PageInfo pageInfo = new PageInfo(all, 5);
        return Msg.success().addExtend("emps", pageInfo);
    }

    /**
     * @Description: 展示所有员工信息
     * @Param: []
     * @return: java.lang.String
     * @Author: zwh
     * @Date: 2019/6/4
     */
//    @RequestMapping("/emps")
    public String getEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        //引入分页插件pagehelper
        //传入页码，以及分页大小
        PageHelper.startPage(pn, 10);
        //startpage后面紧跟的查询就是一个分页查询
        List all = employeeService.getAll();
        //使用 pageinfo封装数据信息
        PageInfo pageInfo = new PageInfo(all, 5);
        //将数据加入model
        model.addAttribute("emps", pageInfo);
        return "list";
    }

}
